{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitec4538a0ed7a4029b9bd19594323cc7e",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "import _pickle as cPickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>country</th>\n      <th>age_group</th>\n      <th>isbn</th>\n      <th>rating</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>usa</td>\n      <td>3</td>\n      <td>0195153448</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>7</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>034542252</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0002005018</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>8</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0060973129</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>8</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0374157065</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1149764</th>\n      <td>278854</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0316184152</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1149765</th>\n      <td>278854</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0375703063</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1149766</th>\n      <td>278854</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>042516098X</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1149767</th>\n      <td>278854</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0425163393</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1149771</th>\n      <td>278854</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0553579606</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n<p>804841 rows × 5 columns</p>\n</div>",
      "text/plain": "         user_id country  age_group        isbn  rating\n0              2     usa          3  0195153448       0\n1              7     usa          0   034542252       0\n2              8  canada          0  0002005018       5\n3              8  canada          0  0060973129       0\n4              8  canada          0  0374157065       0\n...          ...     ...        ...         ...     ...\n1149764   278854     usa          0  0316184152       7\n1149765   278854     usa          0  0375703063       7\n1149766   278854     usa          0  042516098X       7\n1149767   278854     usa          0  0425163393       7\n1149771   278854     usa          0  0553579606       8\n\n[804841 rows x 5 columns]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = cPickle.load(open('data/df_train.pkl','rb'))\n",
    "df_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "<class 'pandas.core.frame.DataFrame'>\nInt64Index: 804841 entries, 0 to 1149771\nData columns (total 5 columns):\nuser_id      804841 non-null int64\ncountry      804841 non-null object\nage_group    804841 non-null int64\nisbn         804841 non-null object\nrating       804841 non-null int64\ndtypes: int64(3), object(2)\nmemory usage: 36.8+ MB\n"
    }
   ],
   "source": [
    "df_train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "usa                    537641\ncanada                  72451\nunited kingdom          35354\ngermany                 27687\n                        19338\n                        ...  \niran\"                       1\nindia\"                      1\nu.s. virgin islands         1\nangola                      1\npetrolwar nation            1\nName: country, Length: 290, dtype: int64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.country.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "['usa',\n 'canada',\n 'united kingdom',\n 'germany',\n '',\n 'australia',\n 'spain',\n 'france',\n 'n/a',\n 'italy',\n 'portugal',\n 'new zealand']"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_country_list= df_train.country.value_counts()[:12].index.tolist()\n",
    "top_country_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "{'australia',\n 'canada',\n 'france',\n 'germany',\n 'italy',\n 'new zealand',\n 'portugal',\n 'spain',\n 'united kingdom',\n 'usa'}"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_country_set = set(top_country_list)\n",
    "top_country_set.remove('')\n",
    "top_country_set.remove('n/a')\n",
    "top_country_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train.country = df_train.country.apply(lambda l : l if l in top_country_set else 'others')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "usa               537641\ncanada             72451\nothers             63812\nunited kingdom     35354\ngermany            27687\naustralia          18604\nspain              17949\nfrance              9735\nitaly               8482\nportugal            7374\nnew zealand         5752\nName: country, dtype: int64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.country.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "feature_recommand = defaultdict(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "australia\n         user_id    country  age_group        isbn  rating\n1120         463  australia          3  0140370900       8\n1367         518  australia          0  0099134012       0\n1368         518  australia          0  0099416158       0\n1730         678  australia          0  0552996092      10\n2286         895  australia          3  0671500546       7\n...          ...        ...        ...         ...     ...\n1149116   278474  australia          3  0192815245       0\n1149118   278474  australia          3  0575068620       6\n1149119   278474  australia          3  1860469922       0\n1149405   278629  australia          2  0747550999       0\n1149594   278766  australia          2  0140340009       0\n\n[18604 rows x 5 columns]\n"
    }
   ],
   "source": [
    "for country, group_by_country in df_train.groupby('country'):\n",
    "    print(country)\n",
    "    print(group_by_country)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "defaultdict(dict,\n            {'australia': {0: {'0099771519': 7.0,\n               '1857022424': 5.333333333333333},\n              1: {},\n              2: {},\n              3: {'0552998486': 8.8,\n               '015602943X': 7.2,\n               '0552140295': 6.8,\n               '0440220602': 6.6,\n               '0446532231': 6.2,\n               '0007170866': 6.142857142857143,\n               '1875989218': 6.090909090909091,\n               '0099727412': 6.0,\n               '0860074382': 5.909090909090909,\n               '0140008535': 5.857142857142857,\n               '0385335482': 5.714285714285714,\n               '0553279556': 5.666666666666667,\n               '1400034779': 5.4,\n               '0743225082': 5.333333333333333,\n               '0949206318': 5.166666666666667},\n              4: {'0091867770': 5.166666666666667},\n              5: {}},\n             'canada': {0: {'0140505865': 9.4,\n               '0140067477': 7.857142857142857,\n               '076790592X': 7.4,\n               '0553250426': 7.125,\n               '0060392452': 7.0,\n               '0676974791': 6.833333333333333,\n               '080410526X': 6.8,\n               '1552783081': 6.8,\n               '0446350982': 6.8,\n               '038079487X': 6.6,\n               '0385505833': 6.4,\n               '0676972152': 6.4,\n               '0786867647': 6.4,\n               '0060959037': 6.375,\n               '0771099894': 6.25,\n               '0812550706': 6.2,\n               '0425122123': 6.2,\n               '0446667900': 6.125,\n               '0671011375': 6.0,\n               '1550541129': 6.0},\n              1: {},\n              2: {},\n              3: {'0060915544': 8.428571428571429,\n               '0140042393': 8.333333333333334,\n               '0552137030': 8.285714285714286,\n               '0836220889': 7.833333333333333,\n               '0425105334': 7.8,\n               '0836218256': 7.666666666666667,\n               '0140277439': 7.6,\n               '0345338588': 7.6,\n               '1853260002': 7.5,\n               '0060935464': 7.4,\n               '0836218353': 7.2,\n               '0671868691': 7.166666666666667,\n               '0446532231': 7.153846153846154,\n               '038542017X': 7.142857142857143,\n               '0440224764': 7.0,\n               '0770422578': 7.0,\n               '0006485278': 7.0,\n               '0451163524': 7.0,\n               '0771060548': 6.888888888888889,\n               '0553287893': 6.833333333333333},\n              4: {'0380718839': 8.4,\n               '0316666343': 7.571428571428571,\n               '0345370775': 7.0,\n               '0684872153': 6.6,\n               '0671758896': 6.2,\n               '0676974805': 6.0,\n               '0440213525': 5.571428571428571,\n               '0515111279': 5.428571428571429,\n               '034540288X': 5.285714285714286,\n               '0676973094': 5.222222222222222,\n               '0671027360': 5.2,\n               '0684874350': 5.0},\n              5: {}},\n             'france': {0: {},\n              1: {},\n              2: {},\n              3: {'2070370003': 9.0,\n               '2070408507': 7.333333333333333,\n               '2253140872': 7.142857142857143,\n               '2266084372': 7.125,\n               '2877302202': 6.8,\n               '22530152730': 6.6,\n               '2070744833': 6.571428571428571,\n               '2253072079': 6.4,\n               '2277260029': 6.4,\n               '2290302155': 6.333333333333333,\n               '2070363317': 6.2,\n               '2253153265': 6.2,\n               '2253144452': 6.166666666666667,\n               '2264016124': 6.166666666666667,\n               '2226135022': 6.125,\n               '2020306492': 6.0,\n               '2253154393': 6.0,\n               '2290308285': 5.888888888888889,\n               '2253150711': 5.714285714285714,\n               '229030039X': 5.6},\n              4: {},\n              5: {}},\n             'germany': {0: {'3518366823': 6.8,\n               '3423072571': 6.2,\n               '3518368540': 6.0,\n               '3423203668': 5.333333333333333,\n               '3426029553': 5.25,\n               '3453007867': 5.166666666666667,\n               '3404148665': 5.0},\n              1: {},\n              2: {},\n              3: {'3551551936': 9.6,\n               '3551551693': 9.5,\n               '3551551677': 9.5,\n               '3423071516': 9.0,\n               '3518372742': 8.0,\n               '3257230478': 7.666666666666667,\n               '3442727952': 7.428571428571429,\n               '3442724686': 7.333333333333333,\n               '3518100734': 7.333333333333333,\n               '3596215226': 7.3,\n               '3150000017': 7.142857142857143,\n               '3426612704': 6.888888888888889,\n               '3423205202': 6.875,\n               '3499224615': 6.714285714285714,\n               '3423202947': 6.7,\n               '3442442354': 6.666666666666667,\n               '3453071174': 6.6,\n               '3551551685': 6.5,\n               '3423204206': 6.444444444444445,\n               '3518388762': 6.4},\n              4: {},\n              5: {}},\n             'italy': {0: {'8806142100': 6.333333333333333,\n               '8826703132': 6.076923076923077,\n               '884590184X': 6.0,\n               '8817131628': 5.2},\n              2: {},\n              3: {'8845205118': 7.666666666666667,\n               '8807813823': 7.2,\n               '8845915700': 7.166666666666667,\n               '8817106259': 6.285714285714286,\n               '8807701510': 6.2,\n               '8807816067': 6.181818181818182,\n               '881786983X': 6.0,\n               '0743222229': 5.857142857142857,\n               '8807817039': 5.833333333333333,\n               '8807814684': 5.5,\n               '8806161741': 5.4,\n               '884541096X': 5.4,\n               '8845290077': 5.285714285714286,\n               '8817106100': 5.25,\n               '0451164075': 5.2,\n               '8807816059': 5.1,\n               '8804342838': 5.090909090909091,\n               '9770390107900': 5.0},\n              4: {},\n              5: {}},\n             'new zealand': {0: {},\n              1: {},\n              2: {},\n              3: {'0156027321': 5.2},\n              4: {},\n              5: {}},\n             'others': {0: {'8826703132': 9.0,\n               '034536676X': 8.166666666666666,\n               '0446604844': 7.8,\n               '0439064872': 7.444444444444445,\n               '0439139600': 7.4,\n               '1558743669': 7.2,\n               '0553375407': 7.2,\n               '0316693251': 7.0,\n               '0060008024': 6.857142857142857,\n               '0688177751': 6.8,\n               '1573229571': 6.8,\n               '0385720106': 6.714285714285714,\n               '0670892963': 6.6,\n               '0385335881': 6.5,\n               '0671001795': 6.5,\n               '0380789019': 6.5,\n               '0446608955': 6.5,\n               '0385484518': 6.5,\n               '0679457526': 6.4,\n               '0316693006': 6.4},\n              1: {},\n              2: {},\n              3: {'0553296981': 9.375,\n               '0441001971': 8.6,\n               '0590353403': 8.5,\n               '0671793489': 8.4,\n               '0553213148': 7.6,\n               '0515131229': 7.4,\n               '0446310786': 7.3,\n               '0439139597': 7.222222222222222,\n               '0345378490': 7.2,\n               '0842329218': 7.142857142857143,\n               '0439136350': 7.1,\n               '0312924585': 7.0,\n               '3257228007': 6.857142857142857,\n               '0786868716': 6.833333333333333,\n               '0151008116': 6.8,\n               '051513628X': 6.8,\n               '0061092177': 6.8,\n               '0747546290': 6.6,\n               '0099800403': 6.5,\n               '0345417623': 6.4},\n              4: {},\n              5: {}},\n             'portugal': {0: {},\n              1: {},\n              2: {},\n              3: {'038082101X': 8.857142857142858,\n               '0439139600': 7.333333333333333,\n               '0380815923': 6.6,\n               '006050918X': 6.4,\n               '9505156944': 6.333333333333333,\n               '0380977788': 5.857142857142857,\n               '002542730X': 5.75,\n               '0460877305': 5.714285714285714,\n               '0385721234': 5.5,\n               '0385413041': 5.4,\n               '9726081378': 5.4,\n               '0811214982': 5.3,\n               '0330262130': 5.2,\n               '0880381744': 5.2,\n               '0552137030': 5.2,\n               '9722113747': 5.166666666666667,\n               '9580464162': 5.0,\n               '9724614565': 5.0,\n               '1401201385': 5.0},\n              4: {},\n              5: {}},\n             'spain': {0: {},\n              1: {},\n              2: {},\n              3: {'8445071416': 8.692307692307692,\n               '8420616893': 8.625,\n               '8420613487': 8.6,\n               '950491036X': 8.428571428571429,\n               '8478886508': 8.4,\n               '8478886451': 8.4,\n               '0156013924': 7.8,\n               '8408039369': 7.8,\n               '8420636282': 7.6,\n               '8495501074': 7.428571428571429,\n               '8420432113': 7.2,\n               '8440627203': 7.166666666666667,\n               '8445071408': 7.0,\n               '0452282152': 7.0,\n               '9681501225': 7.0,\n               '8445071769': 6.8,\n               '8420466034': 6.8,\n               '8445071394': 6.8,\n               '8423309223': 6.625,\n               '8433914545': 6.625},\n              4: {'8408043641': 6},\n              5: {}},\n             'united kingdom': {0: {'0590660543': 6.333333333333333,\n               '000649840X': 6.166666666666667,\n               '0330367358': 6.0,\n               '0330376519': 6.0,\n               '1857027051': 6.0,\n               '0749395990': 5.666666666666667,\n               '0552124753': 5.6,\n               '0099771519': 5.333333333333333,\n               '0749399902': 5.0},\n              1: {},\n              2: {},\n              3: {'1844262553': 8.347826086956522,\n               '0091891965': 8.2,\n               '0140620117': 7.166666666666667,\n               '0330258648': 7.0,\n               '1860465811': 7.0,\n               '1857231384': 7.0,\n               '0552145424': 6.833333333333333,\n               '0440206154': 6.666666666666667,\n               '0552131059': 6.615384615384615,\n               '0192833596': 6.6,\n               '0440235596': 6.6,\n               '0552142379': 6.5,\n               '0330267388': 6.4,\n               '0671024337': 6.4,\n               '0380002930': 6.4,\n               '0722532938': 6.375,\n               '0552149519': 6.2,\n               '0140287248': 6.166666666666667,\n               '0380813815': 6.166666666666667,\n               '0064407667': 6.125},\n              4: {'1844262553': 8.5,\n               '0552147680': 6.6,\n               '0552131067': 5.8,\n               '0552996181': 5.0},\n              5: {}},\n             'usa': {0: {'0156004801': 9.6,\n               '0060256672': 9.333333333333334,\n               '0971942315': 9.0,\n               '1576738167': 9.0,\n               '0553351397': 8.833333333333334,\n               '0441002935': 8.6,\n               '0615116426': 8.454545454545455,\n               '0316735027': 8.333333333333334,\n               '0140309578': 8.333333333333334,\n               '0140185232': 8.2,\n               '0553148001': 8.166666666666666,\n               '0395977894': 8.1,\n               '0743455967': 8.0,\n               '0618002235': 8.0,\n               '039480029X': 8.0,\n               '0877738513': 8.0,\n               '0060248025': 8.0,\n               '0807083054': 7.888888888888889,\n               '0553380958': 7.8,\n               '0553213504': 7.8},\n              1: {'0064407667': 9.6,\n               '0439064872': 8.0,\n               '059035342X': 7.333333333333333},\n              2: {'0590353403': 9.444444444444445,\n               '0440237688': 9.4,\n               '0451191137': 8.8,\n               '0439139597': 8.357142857142858,\n               '0679886370': 8.166666666666666,\n               '0140348107': 8.0,\n               '0064472272': 7.714285714285714,\n               '0439064864': 7.666666666666667,\n               '0671027344': 7.636363636363637,\n               '0446608955': 7.555555555555555,\n               '0394820371': 7.428571428571429,\n               '0385729332': 7.333333333333333,\n               '0316666343': 7.285714285714286,\n               '0385729340': 7.2,\n               '0385730586': 7.0,\n               '043935806X': 6.615384615384615,\n               '0439136350': 6.6,\n               '1576738159': 6.6,\n               '0440439884': 6.4,\n               '0345342968': 6.333333333333333},\n              3: {'0066238501': 9.875,\n               '0517693119': 9.833333333333334,\n               '0394800389': 9.833333333333334,\n               '0920668364': 9.666666666666666,\n               '0395177111': 9.5,\n               '0890876789': 9.4,\n               '0898159954': 9.4,\n               '0316286850': 9.4,\n               '0375503803': 9.2,\n               '0316779059': 9.0,\n               '156402976X': 9.0,\n               '0553110845': 9.0,\n               '0060256737': 8.833333333333334,\n               '0060968966': 8.8,\n               '0670451932': 8.8,\n               '0805013407': 8.6,\n               '0060961325': 8.6,\n               '0140195831': 8.6,\n               '0451525663': 8.6,\n               '0451171926': 8.4},\n              4: {'0312303467': 9.4,\n               '0060514957': 9.0,\n               '0553377868': 8.857142857142858,\n               '0688167888': 8.4,\n               '0399151451': 8.0,\n               '1579546463': 8.0,\n               '0380977311': 7.857142857142857,\n               '0553379615': 7.833333333333333,\n               '0943233828': 7.8,\n               '0446532452': 7.571428571428571,\n               '0553565079': 7.4,\n               '0066214769': 7.4,\n               '0446322180': 7.285714285714286,\n               '0439064864': 7.222222222222222,\n               '0399151613': 7.2,\n               '0449911519': 7.2,\n               '0451169514': 7.166666666666667,\n               '0553802461': 7.125,\n               '0312983220': 7.0,\n               '0312983301': 7.0},\n              5: {'0440234743': 7.6, '044022165X': 6.4}}})"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_recommand = defaultdict(dict)\n",
    "for country, group_by_country in df_train.groupby('country'):\n",
    "    for age, group_by_location_age in group_by_country.groupby('age_group'):\n",
    "        # print(country, age)\n",
    "        item_value_count = group_by_location_age.isbn.value_counts()\n",
    "        divide_boundry = max(np.percentile(item_value_count, 75), 5)\n",
    "        # print(divide_boundry)\n",
    "        # print(item_value_count)\n",
    "        # print(item_value_count[item_value_count>= divide_boundry])\n",
    "\n",
    "        top_books = group_by_location_age[\n",
    "            group_by_location_age.isbn.isin(\n",
    "                item_value_count[item_value_count >= divide_boundry].index\n",
    "            )\n",
    "        ]\n",
    "        # print(top_books.groupby('isbn').rating.mean())\n",
    "        item_mean_rating = top_books.groupby('isbn')['rating'].mean().sort_values(ascending = False)[:20]\n",
    "        # print(item_mean_rating)\n",
    "        # print(item_mean_rating[item_mean_rating > 5])\n",
    "        feature_recommand[country][age] = {}\n",
    "        res = item_mean_rating[item_mean_rating >= 5]\n",
    "        for i in res.index:\n",
    "            feature_recommand[country][age][i] = item_mean_rating[i]\n",
    "    #     break\n",
    "    # break\n",
    "feature_recommand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "dict_keys(['australia', 'canada', 'france', 'germany', 'italy', 'new zealand', 'others', 'portugal', 'spain', 'united kingdom', 'usa'])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_recommand.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "{'0099771519': 7.0, '1857022424': 5.333333333333333}"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_recommand['australia'][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "fetch the global top ratings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0971880107    1745\n0316666343     917\n0385504209     615\n0312195516     522\n0060928336     515\n              ... \n0886777844      37\n0425117383      37\n0679722645      37\n0836218620      37\n0553560271      37\nName: isbn, Length: 2000, dtype: int64"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top2k= df_train.isbn.value_counts()[:2000]\n",
    "top2k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "isbn\n1844262553    8.000000\n0439139597    6.721805\n0439136350    6.553846\n0439064864    6.547826\n0877017883    6.188679\n                ...   \n0061013315    1.023810\n0061092045    1.023810\n0425165701    0.925926\n0679433740    0.914894\n0425173534    0.893617\nName: rating, Length: 2000, dtype: float64"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_top2k = df_train[df_train.isbn.isin(top2k.index)]\n",
    "top2k_rating = df_top2k.groupby('isbn').rating.mean().sort_values(ascending = False)\n",
    "top2k_rating"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "isbn\n1844262553    8.000000\n0439139597    6.721805\n0439136350    6.553846\n0439064864    6.547826\n0877017883    6.188679\n0618002227    6.162791\n0590353403    6.132812\n0671027344    6.073529\n0385199570    5.926829\n0771060548    5.911111\n0156528207    5.777778\n0618129022    5.756098\n0552131059    5.666667\n0811801802    5.650000\n0670032379    5.550000\n0812550706    5.517730\n0670894605    5.419355\n0811825558    5.358974\n0439139600    5.323308\n0440219078    5.300885\nName: rating, dtype: float64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "global_top20 = top2k_rating[top2k_rating >= 5][:20]\n",
    "global_top20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "for l in feature_recommand: \n",
    "    for age in feature_recommand[l]:\n",
    "        # print(age)\n",
    "        # print(len(feature_recommand[l][age]))\n",
    "        fill_size = 20 - len(feature_recommand[l][age])\n",
    "        for i in range(fill_size):\n",
    "            # print(global_top20.index[i], global_top20.iloc[i])\n",
    "            feature_recommand[l][age][global_top20.index[i]] =  global_top20.iloc[i]\n",
    "    # break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "12},\n              1: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              2: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              3: {'038082101X': 8.857142857142858,\n               '0439139600': 7.333333333333333,\n               '0380815923': 6.6,\n               '006050918X': 6.4,\n               '9505156944': 6.333333333333333,\n               '0380977788': 5.857142857142857,\n               '002542730X': 5.75,\n               '0460877305': 5.714285714285714,\n               '0385721234': 5.5,\n               '0385413041': 5.4,\n               '9726081378': 5.4,\n               '0811214982': 5.3,\n               '0330262130': 5.2,\n               '0880381744': 5.2,\n               '0552137030': 5.2,\n               '9722113747': 5.166666666666667,\n               '9580464162': 5.0,\n               '9724614565': 5.0,\n               '1401201385': 5.0,\n               '1844262553': 8.0},\n              4: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              5: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212}},\n             'spain': {0: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              1: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              2: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              3: {'8445071416': 8.692307692307692,\n               '8420616893': 8.625,\n               '8420613487': 8.6,\n               '950491036X': 8.428571428571429,\n               '8478886508': 8.4,\n               '8478886451': 8.4,\n               '0156013924': 7.8,\n               '8408039369': 7.8,\n               '8420636282': 7.6,\n               '8495501074': 7.428571428571429,\n               '8420432113': 7.2,\n               '8440627203': 7.166666666666667,\n               '8445071408': 7.0,\n               '0452282152': 7.0,\n               '9681501225': 7.0,\n               '8445071769': 6.8,\n               '8420466034': 6.8,\n               '8445071394': 6.8,\n               '8423309223': 6.625,\n               '8433914545': 6.625},\n              4: {'8408043641': 6,\n               '1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692},\n              5: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212}},\n             'united kingdom': {0: {'0590660543': 6.333333333333333,\n               '000649840X': 6.166666666666667,\n               '0330367358': 6.0,\n               '0330376519': 6.0,\n               '1857027051': 6.0,\n               '0749395990': 5.666666666666667,\n               '0552124753': 5.6,\n               '0099771519': 5.333333333333333,\n               '0749399902': 5.0,\n               '1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778},\n              1: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              2: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212},\n              3: {'1844262553': 8.347826086956522,\n               '0091891965': 8.2,\n               '0140620117': 7.166666666666667,\n               '0330258648': 7.0,\n               '1860465811': 7.0,\n               '1857231384': 7.0,\n               '0552145424': 6.833333333333333,\n               '0440206154': 6.666666666666667,\n               '0552131059': 6.615384615384615,\n               '0192833596': 6.6,\n               '0440235596': 6.6,\n               '0552142379': 6.5,\n               '0330267388': 6.4,\n               '0671024337': 6.4,\n               '0380002930': 6.4,\n               '0722532938': 6.375,\n               '0552149519': 6.2,\n               '0140287248': 6.166666666666667,\n               '0380813815': 6.166666666666667,\n               '0064407667': 6.125},\n              4: {'1844262553': 8.0,\n               '0552147680': 6.6,\n               '0552131067': 5.8,\n               '0552996181': 5.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005},\n              5: {'1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359,\n               '0439139600': 5.323308270676692,\n               '0440219078': 5.300884955752212}},\n             'usa': {0: {'0156004801': 9.6,\n               '0060256672': 9.333333333333334,\n               '0971942315': 9.0,\n               '1576738167': 9.0,\n               '0553351397': 8.833333333333334,\n               '0441002935': 8.6,\n               '0615116426': 8.454545454545455,\n               '0316735027': 8.333333333333334,\n               '0140309578': 8.333333333333334,\n               '0140185232': 8.2,\n               '0553148001': 8.166666666666666,\n               '0395977894': 8.1,\n               '0743455967': 8.0,\n               '0618002235': 8.0,\n               '039480029X': 8.0,\n               '0877738513': 8.0,\n               '0060248025': 8.0,\n               '0807083054': 7.888888888888889,\n               '0553380958': 7.8,\n               '0553213504': 7.8},\n              1: {'0064407667': 9.6,\n               '0439064872': 8.0,\n               '059035342X': 7.333333333333333,\n               '1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677},\n              2: {'0590353403': 9.444444444444445,\n               '0440237688': 9.4,\n               '0451191137': 8.8,\n               '0439139597': 8.357142857142858,\n               '0679886370': 8.166666666666666,\n               '0140348107': 8.0,\n               '0064472272': 7.714285714285714,\n               '0439064864': 7.666666666666667,\n               '0671027344': 7.636363636363637,\n               '0446608955': 7.555555555555555,\n               '0394820371': 7.428571428571429,\n               '0385729332': 7.333333333333333,\n               '0316666343': 7.285714285714286,\n               '0385729340': 7.2,\n               '0385730586': 7.0,\n               '043935806X': 6.615384615384615,\n               '0439136350': 6.6,\n               '1576738159': 6.6,\n               '0440439884': 6.4,\n               '0345342968': 6.333333333333333},\n              3: {'0066238501': 9.875,\n               '0517693119': 9.833333333333334,\n               '0394800389': 9.833333333333334,\n               '0920668364': 9.666666666666666,\n               '0395177111': 9.5,\n               '0890876789': 9.4,\n               '0898159954': 9.4,\n               '0316286850': 9.4,\n               '0375503803': 9.2,\n               '0316779059': 9.0,\n               '156402976X': 9.0,\n               '0553110845': 9.0,\n               '0060256737': 8.833333333333334,\n               '0060968966': 8.8,\n               '0670451932': 8.8,\n               '0805013407': 8.6,\n               '0060961325': 8.6,\n               '0140195831': 8.6,\n               '0451525663': 8.6,\n               '0451171926': 8.4},\n              4: {'0312303467': 9.4,\n               '0060514957': 9.0,\n               '0553377868': 8.857142857142858,\n               '0688167888': 8.4,\n               '0399151451': 8.0,\n               '1579546463': 8.0,\n               '0380977311': 7.857142857142857,\n               '0553379615': 7.833333333333333,\n               '0943233828': 7.8,\n               '0446532452': 7.571428571428571,\n               '0553565079': 7.4,\n               '0066214769': 7.4,\n               '0446322180': 7.285714285714286,\n               '0439064864': 7.222222222222222,\n               '0399151613': 7.2,\n               '0449911519': 7.2,\n               '0451169514': 7.166666666666667,\n               '0553802461': 7.125,\n               '0312983220': 7.0,\n               '0312983301': 7.0},\n              5: {'0440234743': 7.6,\n               '044022165X': 6.4,\n               '1844262553': 8.0,\n               '0439139597': 6.7218045112781954,\n               '0439136350': 6.553846153846154,\n               '0439064864': 6.547826086956522,\n               '0877017883': 6.188679245283019,\n               '0618002227': 6.162790697674419,\n               '0590353403': 6.1328125,\n               '0671027344': 6.073529411764706,\n               '0385199570': 5.926829268292683,\n               '0771060548': 5.911111111111111,\n               '0156528207': 5.777777777777778,\n               '0618129022': 5.7560975609756095,\n               '0552131059': 5.666666666666667,\n               '0811801802': 5.65,\n               '0670032379': 5.55,\n               '0812550706': 5.5177304964539005,\n               '0670894605': 5.419354838709677,\n               '0811825558': 5.358974358974359}}})"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_recommand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "cPickle.dump(feature_recommand, open('./data/feature_recommand.pkl', 'wb'))"
   ]
  }
 ]
}